﻿using IJhbyService;
using JhbyCommon.CommonModel;
using JhbyHelper;
using JhbyModel.Models;
using JhbyWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JhbyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        jhbyrbacContext _context = null;
        //注入
        IDepartmentService _departmentService;
        public DepartmentController(IDepartmentService departmentService)
        {
            _departmentService = departmentService;
        }
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="departName"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetList")]
        public string GetList(int pageIndex, int pageSize, string departName)
        {
            Response response1 = new Response();
            //token验证 
            int verifytokenid = TokenHelper.VerifyToken(HttpContext.Request);
            response1.Code =  verifytokenid;
            if (verifytokenid == 0)
            {
                response1.data = _departmentService.GetList(pageIndex, pageSize, departName);
            }
            else if (verifytokenid == 1)
            {
                response1.message = "访问用户不存在！";
            }
            else if (verifytokenid == 2)
            {
                response1.message = "Token验证失败！";
            }
            else
            {
                response1.message = "账号已经在其他地方登陆！";
            }
      
            return JsonConvert.SerializeObject(response1);
        }
        /// <summary>
        /// 部门添加/编辑
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Edit")]
        public string Edit(Department department)
        {
            return JsonConvert.SerializeObject(_departmentService.Edit(department));
        }
        [HttpGet]
        [Route("GetDerpartList")]
        /// <summary>
        /// 获取部门下拉
        /// </summary>
        /// <returns></returns>
        public string GetDerpartList()
        {

            return JsonConvert.SerializeObject(_departmentService.GetDerpartList());
        }
    }
}
